<?php

namespace App\Jobs;

use App\Models\GrUserCoupons;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;

class CouponMonitor implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $couponData;
    public $activityId;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($couponData,$activityId)
    {
        $this->couponData = $couponData;
        $this->activityId = $activityId;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $activityId=$this->activityId;
        //将抢购的优惠券添加进入到用户优惠券表
        $create=GrUserCoupons::create($this->couponData);
        //优惠券添加成功后将库存长度减1
        if($create){
            Redis::rpop('rushBuy_number' . $activityId);
        }
    }
}
